package com.sunni.interceptor;

import com.sunni.entity.User;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/*day07-07 验证有没有登录？(session有没有user)*/
public class PrivilegeInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        /*day07-07 验证有没有登录？(session有没有user)*/
        // 判断session里的user是不是null？
            //  是null:没登录,跳到login.jsp 原访问return false。
            //  不是null:登录了,放行return true
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        if (user==null){
            response.sendRedirect(request.getContextPath()+"/login.jsp");
            return false;
        }else{
            return true;
        }
    }


}
